//
//  ContactViewController.m
//  作业tableView通讯录
//
//  Created by dllo on 16/6/17.
//  Copyright © 2016年 zuzhiming. All rights reserved.
//

#import "ContactViewController.h"
#import "CutomCell.h"
#import "DetailViewController.h"

@interface ContactViewController ()<UITableViewDelegate, UITableViewDataSource /*,passValueDelegate*/>

@property (nonatomic, retain) NSDictionary *dictionary;

@property (nonatomic, retain) NSArray *keysArray;

@property (nonatomic, retain) NSDictionary *dic;

@end

@implementation ContactViewController

- (void)dealloc
{
    [_dictionary release];
    [_keysArray release];
    [_dic release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dictionary = [NSDictionary dictionary];
    self.keysArray = [NSArray array];
    
    [self dataHandle];
    
    [self config];
    
    [self createTableView];
    
}

- (void)config
{
    self.view.backgroundColor = [UIColor blueColor];
    
    self.navigationItem.title = @"联系人";
}

//数据处理
- (void)dataHandle
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"plist"];
    self.dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
    //排序
    self.keysArray = [self.dictionary.allKeys sortedArrayUsingSelector:@
                      selector(compare:)];
//    NSLog(@"%@ %d %s", _keysArray, __LINE__, __FUNCTION__);
}


//创建视图
- (void)createTableView
{
    
    UITableView *contactTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 64) style:UITableViewStylePlain];
    
    [self.view addSubview:contactTableView];
    [contactTableView release];
    
    contactTableView.delegate = self;
    contactTableView.dataSource = self;
    
    //注册重用池
    [contactTableView registerClass:[CutomCell class] forCellReuseIdentifier:@"cell"];
    
    
    
}

//设置分区头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.keysArray objectAtIndex:section];
}


//返回分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dictionary.count;
}

//返回行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

//返回每个分区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [_keysArray objectAtIndex:section];
    
    NSArray *array = [self.dictionary objectForKey:key];
    
    return array.count;

}

//重用方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //自定义cell
    CutomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    
//    cell.pvDelegate = self;
    
    NSArray *array = [self.dictionary objectForKey:[self.keysArray objectAtIndex:indexPath.section]];
    
    //根据row行数取出对应的联系人
    self.dic = [array objectAtIndex:indexPath.row];
    
    cell.leftImageView.image = [UIImage imageNamed:[_dic objectForKey:@"photo"]];
    
    cell.nameLabel.text = [_dic objectForKey:@"name"];
    
    return cell;
}

//- (void)passValueWithName:(NSString *)stringName
//{
//    CutomCell *cell = [[CutomCell alloc] init];
//    cell.nameLabel.text = stringName;
//    [cell release];
//}

#pragma mark -- cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *array = [self.dictionary objectForKey:[self.keysArray objectAtIndex:indexPath.section]];
    
    //根据row行数取出对应的联系人
    self.dic = [array objectAtIndex:indexPath.row];

    DetailViewController *dvc = [[DetailViewController alloc] init];

    //传字典
    dvc.dict = _dic;
    [self.navigationController pushViewController:dvc animated:YES];
    
    [dvc release];
}


- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
   return self.keysArray;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
